{% extends "UiManager/base.html" %}
{% block title %}报告管理{% endblock %}
{% block location %}
    <div class="am-icon-home fa fa-home" style="font-weight: bold;color: grey; float: right;">&nbsp;当前位置： 报告管理 > 报告列表</div>
{% endblock %}
{% load staticfiles %}
{% load custom_tags %}
{% block css %}
{% endblock %}
{% block js %}
    <script>

    //搜索中所属项目联动
    $("#newSel").on('click', function () {
        var newNum = $("#newSel option:selected").val();//所属项目
        $("#newpage").find('option').remove();
        $("#newpage").append('<option>所属页面</option>');
        var datas = {
            "pageSize": 99999
        }
        var data = JSON.stringify(datas);
        $.ajax({
            url: '/api/v1/page?p=1',
            type: 'post',
            dataType: 'json',
            data: data,
            success: function (res) {
                for (var i = 0; i < res.data.pages.length; i++) {
                    if (newNum == res.data.pages[i].projectId) {
                        $("#newpage").append('<option ID="' + res.data.pages[i].id + '" value="' + res.data.pages[i].name + '">' + res.data.pages[i].name + '</option>');
                    }
                }

            }
        });
    });

    //新建中所属项目联动
    {#$("#projectSelect").on('change', function () {#}
    {#    $("#testcaseSelect").find("option").remove();#}
    $("#newSel1").on('change', function () {
        var newNum = $("#newSel1 option:selected").val();//所属项目
        $("#newpage1").find('option').remove();
        $("#newpage1").append('<option>所属页面</option>');
        var datas = {
            "project_id": newNum
        }
        var data = JSON.stringify(datas);
        $.ajax({
            url: '/ui/get_page_by_project/',
            type: 'post',
            dataType: 'json',
            data: data,
            success: function (res) {
                console.info(res.content);
                for (var i = 0; i < res.content.length; i++) {
                    $("#newpage1").append('<option value="' + res.content[i]["id"]+ '">' + res.content[i]["page_name"] + '</option>');
                }

            }
        });
    });

    /*新建方法*/
    function NewList() {
        var newName, newpage1, newBy, newLocator, newRemark;
        newName = $("#newName").val();                       //元素名称
        newpage1 = $("#newpage1 option:selected").val();//所属页面名称
        newBy = $("#newBy option:selected").val();   //定位类型
        newLocator = $("#newLocator").val();                 //元素文本
        newRemark = $("#newRemark").val();                   //备注信息
        var datas = {
            "element_name": newName,               //元素名称
            "belong_page": newpage1,            //所属页面名称
            "belong_location": newBy,                   //定位类型
            "element_value": newLocator,         //元素值
            "simple_desc": newRemark,           //备注信息
        };
        var data = JSON.stringify(datas);
        $.ajax({
            url: '/ui/add_element/',
            type: 'post',
            contentType: 'application/json',
            data: data,
            dataType: 'json',
            success: function (res) {
                if (res.code == 0) {
                    //重新获取下列表
                    location.reload();
                    //取消方法
                    closeDown()
                } else {
                    $(".js_message").html(res.message);
                }
            }
        });
    }

    /*新建弹窗取消*/
    function closeDown() {
        $('#newProject').modal('hide');
        $(".js_message").html("");
        $("#newName").val("");
        $("#newSel1 option:first").prop("selected", 'selected');
        $("#newpage1").find('option').remove();
        $("#newpage1").append('<option>所属页面</option>');
        $("#newBy option:first").prop("selected", 'selected');
        $("#newLocator").val("");
        $("#newRemark").val("");
        $("#deleteObj").modal('hide');
        $("#delMessage").html("");
    }

    /*搜索方法*/

    /*编辑页面*/
    function editList(_this) {
        $('#newProject1').modal('show');
        var editID = $(_this).attr('id');
        $("#newSel2,#newpage2").find('option').remove();
        $(".js_BJmessage").html("");
        var datas = {
            "pageSize": 99999
        }
        var data = JSON.stringify(datas);
        $.ajax({
            url: '/api/v1/element?p=1',
            type: 'post',
            data: data,
            dataType: 'json',
            success: function (res) {
                for (var i = 0; i < res.data.elements.length; i++) {
                    if (res.data.elements[i].id == editID) {
                        pageID = editID;
                        $("#newName1").val(res.data.elements[i].name);                       //元素名称
                        $("#newSel2").append('<option ID="' + res.data.elements[i].id + '" value="' + res.data.elements[i].projectName + '">' + res.data.elements[i].projectName + '</option>');
                        $("#newSel2 option[value='" + res.data.elements[i].projectName + "']").attr("selected", true);//所属项目
                        $("#newpage2").append('<option ID="' + res.data.elements[i].id + '" value="' + res.data.elements[i].pageName + '">' + res.data.elements[i].pageName + '</option>');
                        $("#newpage2 option[value='" + res.data.elements[i].pageName + "']").attr("selected", true);//所属页面
                        $("#newBy1 option[value='" + res.data.elements[i].by + "']").attr("selected", true);//定位类型
                        $("#newLocator1").val(res.data.elements[i].locator);                 //元素定位
                        $("#newRemark1").val(res.data.elements[i].remark);                   //备注信息

                    }
                }

            }
        });
    }

    /*删除询问方法*/
    function isDel(id) {
        $("#delButton").attr("onclick", "del(" + id + ")");
        $("#deleteObj").modal("show");
    }

    /*删除方法*/
    function del(id) {
        var data = {
          "id": id,
          "type": "delete"
        };
        $.ajax({
            url: '/ui/edit_report/',
            type: 'post',
            contentType: 'application/json',
            dataType: 'json',
            data:JSON.stringify(data),
            success: function (res) {
                if (res.code == 0) {
                    $("#deleteObj").modal("hide");
                    location.reload();
                } else {
                    $("#delMessage").html(res.message);
                }
            }
        });
    }
</script>
{% endblock %}
{% block content %}
        <div id="page-inner">
            <div class="row">
                <div class="col-md-12">
                    <div class="panel panel-default">
                        <div class="panel-body" style="min-height:500px">
                            <div class="row mar-bottom-20">
                                <div class="form-inline">
                                    <form id="pro_filter" method="POST" action="/ui/report_list/">
                                        <div class="col-lg-12">
                                            <div class="input-group">
                                              <input name="search_report_name" type="text" class="form-control" placeholder="报告名称" value="{{ search_report_name }}">
                                                <span class="input-group-btn">
                                                <button class="btn btn-primary" type="submit">搜索</button>
                                              </span>
                                            </div><!-- /input-group -->
                                        </div><!-- /.col-lg-12 -->
                                    </form>
                                </div>
                            </div>
                            <!-- Advanced Tables -->
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover" id="dataTables-example">
                                    <thead>
                                    <tr>
                                        <th>序号</th>
                                        <th>运行套件</th>
                                        <th>运行时间</th>
                                        <th>运行个数</th>
                                        <th>成功个数</th>
                                        <th>失败个数</th>
                                        <th>成功率</th>
                                        <th>操作</th>
                                    </tr>
                                    </thead>
                                    <tbody class="js_table">
                                    {% for row in obj %}
                                        <tr>
                                        <td>{{ row.id }}</td>
                                        <td>{{ row.html_report_name }}</td>
                                        <td>{{ row.start_at }}</td>
                                        <td>{{ row.testsRun }}</td>
                                        <td>{{ row.successes }}</td>
                                        <td>{{ row.failures }}</td>
                                        <td>{{ row.percent|convert_integer }}%</td>
                                        <td>
                                            <a href="/ui/view_report/?report_path={{ row.report_path }}" class="btn-sm btn-primary" target="_blank">详情</a>
                                            <button class="btn-danger" onclick="isDel('{{ row.id }}')">删除</button>
                                        </td>
                                    </tr>
                                    {% endfor %}
                                    </tbody>
                                </table>
                            </div>
                            <!--End Advanced Tables -->
                            <ul class="pagination" id="pager">
                                {#上一页按钮开始#}
                                {# 如果当前页有上一页#}
                                {% if obj.has_previous %}
                                    {#  当前页的上一页按钮正常使用#}
                                    <li class="previous"><a href="/ui/report_list?page={{ obj.previous_page_number }}">&laquo;</a></li>
                                {% else %}
                                    {# 当前页的不存在上一页时,上一页的按钮不可用#}
                                    <li class="previous disabled"><a href="#">&laquo;</a></li>
                                {% endif %}
                                {#上一页按钮结束#}
                                {# 页码开始#}
                                {% for num in paginator.page_range %}

                                    {% if num == currentPage %}
                                        <li class="item active"><a href="/ui/report_list?page={{ num }}">{{ num }}</a></li>
                                    {% else %}
                                        <li class="item"><a href="/ui/report_list?page={{ num }}">{{ num }}</a></li>
                                    {% endif %}
                                {% endfor %}
                                {#页码结束#}
                                {# 下一页按钮开始#}
                                {% if obj.has_next %}
                                    <li class="next"><a href="/ui/report_list?page={{ obj.next_page_number }}">&raquo;</a></li>
                                {% else %}
                                    <li class="next disabled"><a href="#">&raquo;</a></li>
                                {% endif %}
                                {# 下一页按钮结束#}
                            </ul>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <!--删除询问弹窗提示-->
        <div class="modal fade" id="deleteObj" tabindex="-1" role="dialog" aria-labelledby="myModalLabel1"
             aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                            &times;
                        </button>
                        <h4 class="modal-title">删除报告</h4>
                    </div>
                    <div class="modal-body">
                        <p>请确认是否删除该报告</p>
                        <p id='delMessage' class="js_DELmessage text-danger"></p>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" onclick="closeDown()">取消</button>
                        <button type="button" class="btn btn-primary" id="delButton">确定</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal -->
        </div>
        <!--end 删除询问弹窗提示-->
{% endblock %}